/**
 * Created by Mark_ren on 2016/10/25.
 * 生活本不应如此坎坷
 */
var express = require('express');
var router = express.Router();
var co = require('co');
var seqModule = require('./createModules');
var seq = new seqModule();

//添加评论
router.get('/addComment', function(req, res, next) {
    co(function () {
        seq.commentModule.create(req.query).then(function(results){
            res.json({"success":true,"rows":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'添加失败'});
        console.log(e);
    })
});
//删除评论
router.get('/deleteComment', function(req, res, next) {
    co(function () {
        var params=req.query.params;
        var arr=params.split(",");
        var paramT=[arr];
        seq.commentModule.destroy({'where':{'id':paramT}}).then(function(results){
            res.json({"success":true,"msg":"删除成功","rows":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'删除失败'});
        console.log(e);
    })
});
//微信返回评论列表
router.get('/getComment', function(req, res, next) {
    co(function () {
        var courseId=req.query.courseId;
        var qtype=req.query.type;
        seq.sequelize.query("select q.id, q.course_id as courseId,q.member_id as memberId,q.member_picture as memberPicture,q.member_content as memberContent ," +
            " q.create_time as createTime,m.open_id as openId,m.name,m.hospital_name as hospitalName,m.hospital_offices as hospitalOffices  from  qcc_comment as q,member as m where q.member_id=m.open_id " +
            "and q.type = '"+qtype+"' and q.course_id= '"+courseId+"' order by q.create_time desc;").spread(function(results){
            res.json({"success":true,"msg":"查询成功","result":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});

//PC返回评论数与点击量列表
router.get('/getCommentAndLog', function(req, res, next) {
    co(function () {
        var page=parseInt(req.query.page);
        var rows=parseInt(req.query.rows);
        var total=0;
        var params="";
        if(req.query.type!=null&&req.query.type!=''&&req.query.type!=0){
            params=" and type ="+req.query.type  ;
        }
        if(req.query.title!=null&&req.query.title!=''){
            params+=" and title like '%"+req.query.title+"%'";
        }
        seq.sequelize.query("select count(id) as total from (select class.id,class.TITLE,class.CREATETIME, " +
            " (select count(id) from qcc_comment where class.id = course_id LIMIT 1) as pinglun," +
            "(select count(id) from qcc_log where class.id=title LIMIT 1) as dianji  from " +
            "(select id,title, CREATETIME ,1001 as type from tp_ccdt.qcc_classroom  " +
            " union all select id,title, CREATETIME , 1002 as type  " +
            " from tp_ccdt.qcc_discuss union ALL" +
            "  select id,title, CREATETIME , 1003 as type from tp_ccdt.qcc_news  )  as class " +
            "where 1=1 "+params+"  order BY class.CREATETIME desc) as cl LIMIT 1 ").spread(function(result){

            total=result[0].total;
        });

        seq.sequelize.query("select class.id,class.TITLE,class.CREATETIME,  type, " +
            " (select count(id) from qcc_comment where class.id = course_id LIMIT 1) as pinglun," +
            " (select count(id) from qcc_log where class.id=title LIMIT 1) as dianji  from " +
            " (select id,title, CREATETIME ,1001 as type from tp_ccdt.qcc_classroom  " +
            " union all select id,title, CREATETIME , 1002 as type  " +
            " from tp_ccdt.qcc_discuss union ALL" +
            "  select id,title, CREATETIME , 1003 as type from tp_ccdt.qcc_news  )  as class " +
            "where 1=1 "+params+"  order BY class.CREATETIME desc limit "+page*rows+","+(page+1)*rows+";").spread(function(results){
            res.json({"success":true,"total":total,"msg":"查询成功","result":results});

        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});


//PC返回评论列表
router.get('/getCommentForPc', function(req, res, next) {
    co(function () {
        var page=parseInt(req.query.page);
        var rows=parseInt(req.query.rows);
        var total=0;
        seq.sequelize.query("select count(id) as total from (select q.id, q.course_id as courseId,q.member_id as memberId,q.member_picture as memberPicture,q.member_content as memberContent ," +
            " q.create_time as createTime,m.open_id as openId,m.name,m.hospital_name as hospitalName,m.hospital_offices as hospitalOffices  from  qcc_comment as q,member as m where q.member_id=m.open_id " +
             " and q.member_content like '%"+req.query.title+"%'and q.course_id= '"+req.query.courseId+"' order by q.create_time desc) as q limit 1 ;").spread(function(result){
            total=result[0].total;
        });
        seq.sequelize.query("select q.id, q.course_id as courseId,q.member_id as memberId,q.member_picture as memberPicture,q.member_content as memberContent ," +
            " q.create_time as createTime,m.open_id as openId,m.name,m.hospital_name as hospitalName,m.hospital_offices as hospitalOffices  from  qcc_comment as q,member as m where q.member_id=m.open_id " +
            " and q.member_content like '%"+req.query.title+"%' and q.course_id= '"+req.query.courseId+"' order by q.create_time desc limit "+page*rows+","+(page+1)*rows+";").spread(function(result){
        res.json({"success":true,"total":total,"msg":"查询成功","result":result});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});


module.exports = router;